package com.jing.yao.utils;

import com.jing.yao.annotation.Formatter;
import com.jing.yao.cache.CacheHelper;
import com.jing.yao.cache.RedisKeyEnum;
import com.jing.yao.constant.Enabled;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 字段翻译
 * @author yao
 * @since 2020-07-14
 */
public class YaoBeanUtils {

    /**
     * 字段类型转换
     * @param source
     * @param target
     * @param <T>
     * @return
     */
    public static  <T> T copyAndFormatter(Object source, T target) {

        BeanUtils.copyProperties(source, target);

        //获取目标类并翻译
        Field[] declaredFields = target.getClass().getDeclaredFields();
        //遍历字段，找出 Formatter注解注释的字段
        Arrays.stream(declaredFields).parallel().forEach(field -> {
            try {
                //判断是否有注解Formatter
                if (field.isAnnotationPresent(Formatter.class)) {
                    PropertyDescriptor descriptor = new PropertyDescriptor(field.getName(), source.getClass());
                    Method readMethod = descriptor.getReadMethod();
                    //读取属性值
                    Object result = readMethod.invoke(source);
                    if ( result instanceof Boolean) {
                        result = (Boolean)result ? Enabled.YES.getValue() : Enabled.NO.getValue();
                    }
                    //非空判断
                    if ( result != null) {
                        Formatter annotation = field.getAnnotation(Formatter.class);

                        CacheHelper cacheHelper = ApplicationContextUtils.getBean(CacheHelper.class);


                        String key = "";
                        if(StringUtils.isBlank(annotation.key())) {
                            String dictCode = annotation.dictCode();
                            //获取对应字典中的值
                            key = RedisKeyEnum.DICT_PREFIX.getKey() + dictCode;
                        } else {
                            key = annotation.key();
                        }

                        String dictValue = cacheHelper.hashGet(key).get(result.toString());

                        PropertyDescriptor descriptorTarget = new PropertyDescriptor(field.getName(), target.getClass());
                        if (StringUtils.isBlank(annotation.targetField())) {
                            Method writeMethod = descriptorTarget.getWriteMethod();
                            writeMethod.invoke(target, dictValue);
                        } else {
                            descriptorTarget = new PropertyDescriptor(annotation.targetField(), target.getClass());
                            if (descriptorTarget != null) {
                                descriptorTarget.getWriteMethod().invoke(target, dictValue);
                            }
                        }
                    }

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        return target;
    }
}
